package com.xuecheng.teaching.controller;

import com.xuecheng.api.comment.model.dto.CommentDTO;
import com.xuecheng.api.comment.model.dto.CommentReplyDTO;
import com.xuecheng.api.comment.model.qo.CommentConditionModel;
import com.xuecheng.api.comment.model.vo.CommentStatisticsVo;
import com.xuecheng.api.teaching.CompanyApi;
import com.xuecheng.api.teaching.model.CompanyDTO;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.teaching.common.utils.UAASecurityUtil;
import com.xuecheng.teaching.service.CompanyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 教育机构 前端控制器
 */
@Slf4j
@RestController
public class CompanyController implements CompanyApi {

    @Autowired
    private CompanyService companyService;

    @GetMapping("l/company/{tenantId}")
    public CompanyDTO getByTenantId(@PathVariable("tenantId") Long tenantId) {
        return companyService.getByTenantId(tenantId);
    }

    //点赞
    @PostMapping("course-comment/praise/{commentId}")
    public Boolean praise(@PathVariable Long commentId) {
        Boolean result = companyService.praise(commentId);
        return result;
    }


    @PostMapping("course-comment")
    public CommentDTO comment(@Valid @RequestBody CommentDTO commentDTO) {

        String username = UAASecurityUtil.getUser().getUsername();

        CommentDTO comment = companyService.comment(commentDTO, username);

        return comment;
    }

    /*
     * 统计
     * */
    @Override
    @GetMapping("course-comment/course-statistics/{coursePubId}")
    public CommentStatisticsVo getStatistics(@PathVariable Long coursePubId) {

        CommentStatisticsVo commentStatistics = companyService.getStatistics(coursePubId);
        return commentStatistics;
    }

    //删除评论
    @DeleteMapping("m/course-comment/{commentId}")
    public Boolean deleteComment(@PathVariable Long commentId) {
        Boolean result = companyService.deleteComment(commentId);
        return result;
    }

    /**
     * 批量删除
     *
     * @param commentIds
     * @return
     */
    @DeleteMapping("m/course-comment/batch-del/{commentIds}")
    public Boolean batchDelComment(@PathVariable List<Long> commentIds) {

        Boolean result = companyService.batchDelComment(commentIds);

        return result;

    }

    /*
     * 查询评论-用户
     * */
    @PostMapping("course-comment/for-course")
    public PageVO<CommentDTO> commentList(PageRequestParams params, @Valid @RequestBody CommentConditionModel commentModel) {

        commentModel.setComeFrom(CommentDTO.COME_FROM);
        //用户不需要所属机构:false
        return companyService.commentList(params, commentModel, false);
    }

    /*
     * 查询评论-机构
     * */
    @PostMapping("course-comment/list")
    public PageVO<CommentDTO> commentList4c(PageRequestParams params, @Valid @RequestBody CommentConditionModel commentModel) {

        commentModel.setComeFrom(CommentDTO.COME_FROM);

        //只能查询所属机构
        Long companyId = UAASecurityUtil.getCompanyId();
        commentModel.setBelongTo(companyId);
        return companyService.commentList(params, commentModel, true);
    }

    /*
    查询评论-运营平台
    * */
    @GetMapping("m/course-comment/list-all")
    public PageVO<CommentDTO> commentList4y(PageRequestParams params) {
        //commentConditionModel.setComeFrom(CommentDTO.COME_FROM);

        CommentConditionModel conditionModel = new CommentConditionModel();

        conditionModel.setComeFrom(CommentDTO.COME_FROM);

        System.out.println("111");

        //用户不需要所属机构:false
        return companyService.commentList(params, conditionModel, false);
    }


    @PostMapping("course-comment-reply")
    public CommentReplyDTO commentReply(@RequestBody CommentReplyDTO dto) {
        //获得机构id
        Long companyId = UAASecurityUtil.getCompanyId();

        return companyService.commentReply(dto, companyId);
    }

    @GetMapping("company/{companyId}")
    public CompanyDTO getCompanyById(@PathVariable Long companyId) {

        return companyService.getCompanyById(companyId);
    }

    @GetMapping("my-company")
    public CompanyDTO getMyCompany() {
        //获得机构id
        Long companyId = UAASecurityUtil.getCompanyId();

        return companyService.getMyCompany(companyId);
    }

    @PostMapping("personal-org")
    public CompanyDTO createPersonalData(@RequestBody CompanyDTO companyDTO) {

        return null;
    }
}
